package com.eascs.web.o2o.web;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.ConnectionPoolDataSource;
import com.eascs.app.dao.DataSourceSetting;
import com.eascs.app.dao.JdbcSession;
import com.eascs.notify.email.entity.EmailSourceSetting;
import com.eascs.web.DefaultAppConfig;

@Configuration
public class AppConfig extends DefaultAppConfig {
	@Bean
	public EmailSourceSetting emailSourceSetting() {
		EmailSourceSetting emailSourceSetting = new EmailSourceSetting();
		ConfigContext.bindConfigObject(emailSourceSetting,"email.");
		return emailSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db1.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSource dataSource() {
		DataSourceSetting dataSourceSetting = dataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}


	@Bean
	public JdbcSession jdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSource());
		return jdbcSession;
	}

	@Bean
	public DataSourceSetting dataSourceSettingOracle() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db2.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceOracle() {
		DataSourceSetting dataSourceSetting = dataSourceSettingOracle();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "jdbcSessionOracle")
	public JdbcSession jdbcSessionOracle() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceOracle());
		return jdbcSession;
	}

	
	@Bean
	public DataSourceSetting dataSourceSettingDbForWorkflow() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db5.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSource dataSourceDbForWorkflow() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForWorkflow();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	
	
	//o2oservice 配置
	
	@Bean
	public DataSourceSetting dataSourceSettingO2OService() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db4.");
		return dataSourceSetting;
	}
	
	
	
	@Bean
	public DataSource dataSourceO2OService() {
		DataSourceSetting dataSourceSetting = dataSourceSettingO2OService();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	@Bean
	public DataSourceSetting dataSourceSettingO2OCrm() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db6.");
		return dataSourceSetting;
	}
	@Bean
	public DataSource dataSourceO2OCrm() {
		DataSourceSetting dataSourceSetting = dataSourceSettingO2OCrm();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	@Bean(name = "jdbcSessionO2OService")
	public JdbcSession jdbcSessionO2OService() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceO2OService());
		return jdbcSession;
	}
	
	
	

	@Bean(name = "jdbcSessionForWorkflow")
	public JdbcSession jdbcSessionDbForWorkflow() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForWorkflow());
		return jdbcSession;
	}
	
	@Bean(name = "jdbcSessionO2OCrm")
	public JdbcSession jdbcSessionO2OCrm() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceO2OCrm());
		return jdbcSession;
	}
	
	@Bean(name = "transactionManagerDbForWorkflow")
	public PlatformTransactionManager transactionManagerDbForWorkflow() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForWorkflow());
		return transactionManager;
	}
	
	@Bean(name = "transactionManagerDbForAdmain")
	public PlatformTransactionManager transactionManagerDbForAdmain() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceO2OService());
		return transactionManager;
	}
	@Bean(name = "transactionManagerDbForCrm")
	public PlatformTransactionManager transactionManagerDbForCrm() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceO2OCrm());
		return transactionManager;
	}
	
	
	
	
	
	
	
	
	
	
	@Bean
	public DataSourceSetting dataSourceSettingBidm() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db7.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceBidm() {
		DataSourceSetting dataSourceSetting = dataSourceSettingBidm();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "jdbcSessionBidm")
	public JdbcSession jdbcSessionBidm() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceBidm());
		return jdbcSession;
	}
	
	

	@Bean
	public DataSourceSetting dataSourceSettingBiods() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db8.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceBiods() {
		DataSourceSetting dataSourceSetting = dataSourceSettingBiods();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "jdbcSessionBiods")
	public JdbcSession jdbcSessionBiods() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceBiods());
		return jdbcSession;
	}
	
	
	
	@Bean
	public DataSourceSetting dataSourceSettingBidw() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db9.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceBidw() {
		DataSourceSetting dataSourceSetting = dataSourceSettingBidw();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "jdbcSessionBidw")
	public JdbcSession jdbcSessionBidw() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceBidw());
		return jdbcSession;
	}
		

	@Bean
	public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
}
